
#include "BalanceType.h"
#include <iostream>
#include <string>
#include <sstream>

#include <stdlib.h>

////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  BalanceType::BalanceType ( ) : mBalance (0) {}
  BalanceType::BalanceType ( const BalanceType & bt ) { *this = bt ; }

  BalanceType::~BalanceType (  ) {  }

////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  void BalanceType::SetBalanceFromFloat ( float    newBalance ) 
  {
    this -> mBalance = newBalance*100 ;
  }

  void BalanceType::SetBalanceFromInt   ( int32_t newBalance ) 
  {
    this -> mBalance = newBalance ;
  }
  
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  // Take a const-reference to the right-hand side of the assignment.
  // Return a non-const reference to the left-hand side.
  /*
  BalanceType& BalanceType::operator+(const BalanceType &rhs) {

this->dump ( "Add" ) ; rhs.dump ( "rhs" ) ;
    return BalanceType(*this) += rhs;

}
* */


////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  // Take a const-reference to the right-hand side of the assignment.
  // Return a non-const reference to the left-hand side.
  BalanceType& BalanceType::operator+=(const BalanceType &rhs) {
    
    this -> mBalance += rhs.mBalance ;

    return *this;  // Return a reference to myself.
  }

////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  // Take a const-reference to the right-hand side of the assignment.
  // Return a non-const reference to the left-hand side.
  /*
  BalanceType& BalanceType::operator-(const BalanceType &rhs) {
    
    return BalanceType(*this) -= rhs;
  }
*/
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  // Take a const-reference to the right-hand side of the assignment.
  // Return a non-const reference to the left-hand side.
  BalanceType& BalanceType::operator-=(const BalanceType &rhs) {
    
    this -> mBalance -= rhs.mBalance ;

    return *this;  // Return a reference to myself.
  }


////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
  // Converesion operator to get  the value of the balance.
  BalanceType::operator double () 
  {
      return ((double)mBalance)/100 ;
  }
  
////////////////////////////////////////////////////////////////////////////////
/// Get the value of Balance as $xxx.yy
////////////////////////////////////////////////////////////////////////////////
    std::string BalanceType::GetValue ()
    {
      std::string returnValue ( "" ) ;
      
      std::ostringstream convert;   // stream used for the conversion

      convert << mBalance ;  

      returnValue = convert.str() + returnValue ;

      // Need to add decimal point ... it ain't gonna work right for a negative number
      // fix this
      while ( returnValue.length () < 2 )
      {
        returnValue = returnValue + '0' ;
      }
      returnValue = returnValue.substr (0, returnValue.length ()-2 ) + '.' + returnValue.substr ( returnValue.length ()-2, 2 ) ;

      return returnValue ;
    }
  
